package com.ixuea.courses.mycloudmusicapisp.service;

import com.ixuea.courses.mycloudmusicapisp.domain.User;
import com.ixuea.courses.mycloudmusicapisp.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ixuea.courses.mycloudmusicapisp.util.Constant.RESULT_OK;

/**
 * 用户服务
 */
@Service
public class UserService {
    /**
     * 用户mapper
     */
    @Autowired
    private UserMapper mapper;

    /**
     * 查询所有用户
     * @return
     */
    public List<User> findAll() {
        return mapper.findAll();
    }

    /**
     * 根据手机号查询
     * @param data
     * @return
     */
    public User findByPhone(String data) {
        return mapper.findByPhone(data);
    }

    /**
     * 根据邮箱查询
     * @param data
     * @return
     */
    public User findByEmail(String data) {
        return mapper.findByEmail(data);
    }

    /**
     * 创建用户
     * @param data
     * @return
     */
    public boolean create(User data) {
        return mapper.create(data)  == RESULT_OK;
    }

    /**
     * 根据ID查询
     * @param data
     * @return
     */
    public User find(String data) {
        return mapper.find(data);
    }

    /**
     * 根据 qq_id 查询
     * @param data
     * @return
     */
    public User findByQQid(String data) {
        return mapper.findByQQid(data);
    }

    /**
     * 根据 weibo_id查询
     * @param data
     * @return
     */
    public User findByWeiboId(String data) {
        return mapper.findByWeiboId(data);
    }

    /**
     * 更新用户
     *
     * @param data
     * @return
     */
    public boolean update(User data) {
        int result = mapper.update(data);

        return result == RESULT_OK;
    }

    /**
     * 更新第三方账号
     *
     * @param id
     * @param account
     * @param accountDigest
     * @param accountField
     * @return
     */
    public boolean updateAccount(String id, String account, String accountDigest, String accountField) {
        return mapper.updateAccount(id, account, accountDigest, accountField) == RESULT_OK;
    }
}
